package com.example.system.security.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.math.BigInteger;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 封装登录用户信息及权限
 */
@Getter
@AllArgsConstructor
public class LoginUserDetails implements UserDetails {
    
    private final BigInteger userId;
    private final String username;
    private final String password;
    private final BigInteger enabled;
    private final List<String> permissions;
    
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return permissions.stream()
                   .map(p -> (GrantedAuthority) () -> p)
                   .collect(Collectors.toList());
    }
    
    @Override
    public boolean isEnabled() {
        return enabled.equals(new BigInteger("1"));
    }
}
